<?php
declare (strict_types = 1);

namespace app\controller;

use app\common\model\ArticleCate;
use app\common\model\User;
use think\facade\Db;
use think\facade\View;
use think\Request;

class AdminUser extends AdminInit
{
    public function __construct()
    {
        parent::__construct();


    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        $keyword = request()->param("keyword");
        $search_field = request()->param("search_field");
        if($keyword && $search_field){
            if ($search_field == 'id'){
                $where[] = ["{$search_field}",'=',"{$keyword}"];
            }else{
                $where[] = ["{$search_field}",'like',"%{$keyword}%"];
            }
        }
        $type = input("get.type");
        if($type == "robot"){
            //$where["username"]  = ["like","%robot%"];
            $where[] = ["username",'like',"%robot%"];
        }
        $is_admin = input("get.is_admin");
        if($is_admin){
            $where["is_admin"] = 1;
        }

        if(!isset($where)){
            $where = 1;
        }
        //        父级分类
        $list = User::with(["level"])->where($where)->order("last_time DESC,id DESC")
            ->paginate(["query"=>request()->param()]);
        View::assign("list",$list);
        View::assign("title","用户列表");

        return  view();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        View::assign("title","文章发布");
        return \view();
    }



    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
       
        $user = $this->user;
        if(!$user){
            return redirect((string) url('index/index',['redirect' => 'index/user/index']));
        }
        $d = input("param.");
        if(!isset($d["status"])){
            $d["status"] = 0;
        }
        if($d["status"] == 'on'){
            $d["status"] = 1;
        }else{
            $d["status"] = 0;
        }
        if(!isset($d["password"])){
            $d["password"] = md5("123456");
        }
        $res = User::create($d);
        if($res){
            return  json(["code"=>200,"msg"=>"成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }
    }

  

    

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $res = ArticleCate::find($id);
        View::assign("res",$res);
        return view('create');
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $res = ArticleCate::where("id",$id)->delete();
        if($res){
            return  json(["code"=>200,"msg"=>"成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }

    }

}
